import * as Cesium from 'cesium';
import { CzmESGeoVector } from "../../base";
import { ESGeoPolygon } from "earthsdk2s";
import { createNextAnimateFrameEvent } from 'xbsj-base';
import { ESCesiumViewer } from '@/ESCesiumViewer';
export class CzmESGeoPolygon extends CzmESGeoVector<ESGeoPolygon> {
    static readonly type = this.register(ESGeoPolygon.type, this);

    constructor(sceneObject: ESGeoPolygon, czmViewer: ESCesiumViewer) {
        super(sceneObject, czmViewer);
        const viewer = czmViewer.viewer;
        if (!viewer) {
            console.warn(`viewer is undefined!`);
            return;
        }
        const entity = viewer.entities.add({
            polygon: {
                height: 0
            }
        });
        this.d(() => viewer.entities.remove(entity));

        {
            const updateProp = () => {
                if (!sceneObject.points) return;
                const hierarchy = sceneObject.points && Cesium.Cartesian3.fromDegreesArrayHeights([...sceneObject.points].flat());
                //@ts-ignore
                entity.polygon.hierarchy = hierarchy;
            };
            updateProp();
            this.d(sceneObject.pointsChanged.don(updateProp));
        }

        {
            const updateProp = () => {
                //@ts-ignore
                entity.polygon.outline = sceneObject.stroked;
                //@ts-ignore
                entity.polygon.outlineWidth = sceneObject.strokeStyle.width;
                const color = sceneObject.strokeStyle.color;
                //@ts-ignore
                entity.polygon.outlineColor = Cesium.Color.fromBytes(color[0] * 255, color[1] * 255, color[2] * 255, color[3]);
            }
            const updateEvent = this.dv(createNextAnimateFrameEvent(
                sceneObject.strokedChanged,
                sceneObject.strokeStyleChanged,
            ));
            updateProp();
            this.dispose(updateEvent.don(updateProp));
        }

        this.d(sceneObject.flyToEvent.don(() => { viewer.flyTo(entity) }));
    }

}
